package com.yty.newconsult.controller;

import com.yty.newconsult.common.Result;
import com.yty.newconsult.common.ResultGenerator;
import com.yty.newconsult.entity.LoginRequest;
import com.yty.newconsult.entity.User;
import com.yty.newconsult.service.UserService;
import com.yty.newconsult.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/auth")
public class AuthController {

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(@RequestBody LoginRequest loginRequest) {
        System.out.println("登录请求");
        System.out.println("123:"+loginRequest);
        // 1. 用户名密码验证
        User user=userService.queryByName(loginRequest.getUserId());
        if(user.getPassword().equals(loginRequest.getPassword())){
            Map<String,String> payload=new HashMap<>();
            payload.put("id",user.getUserId());
            payload.put("password",user.getPassword());
            //生成token
            String token=JwtTokenUtil.getToken(payload);
            System.out.println("token:"+token);
            return ResultGenerator.genSuccessResult(token);
        }else{
            return ResultGenerator.genFailResult("密码错误");
        }

    }
}
